package com.wyh.service.serviceImpl.groupOne.customer;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyh.common.enums.ResultCodeEnum;
import com.wyh.common.exception.CustomException;
import com.wyh.domain.vo.groupOne.customer.MasterDataCustomerVO;
import com.wyh.domain.vo.groupOne.customer.InvoiceheaderPageVO;
import com.wyh.domain.vo.groupOne.customer.MasterDataInvoiceheaderVO_1;
import com.wyh.mapper.groupOne.customer.MasterDataCCustomerMapper;
import com.wyh.mapper.groupOne.customer.MasterDataCInvoiceheaderMapper;
import com.wyh.service.groupOne.customer.MasterDataCInvoiceheaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MasterDataCInvoiceheaderServiceImpl implements MasterDataCInvoiceheaderService {

    @Resource
    private MasterDataCInvoiceheaderMapper masterDataCInvoiceheaderMapper;

    @Autowired
    private MasterDataCCustomerMapper customerMapper;

    @Override
    public List<MasterDataInvoiceheaderVO_1> selectAll(Integer id) {
        if (id != null) {
            MasterDataCustomerVO dbcustomer = customerMapper.selectDetailBasic(id);
            if (!ObjectUtil.isEmpty(dbcustomer)) {
                List<MasterDataInvoiceheaderVO_1> masterDataInvoiceheaderVO1List = masterDataCInvoiceheaderMapper.selectAll(id);
                return masterDataInvoiceheaderVO1List;
            }
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        throw new CustomException(ResultCodeEnum.PARAM_LOST_ERROR);
    }

    @Override
    public int insert(MasterDataInvoiceheaderVO_1 masterDataInvoiceheaderVO1) {
        if (masterDataInvoiceheaderVO1.getUserid() != null) {
            MasterDataCustomerVO dbcustomer = customerMapper.selectDetailBasic(masterDataInvoiceheaderVO1.getCustomerid());
            if (!ObjectUtil.isEmpty(dbcustomer)) {
                DateTime date = DateUtil.date();
                dbcustomer.setLastupdatetime(date.toString());
                dbcustomer.setUserid(masterDataInvoiceheaderVO1.getUserid());
                customerMapper.update(dbcustomer);
                return masterDataCInvoiceheaderMapper.insert(masterDataInvoiceheaderVO1);
            }
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        throw new CustomException(ResultCodeEnum.PARAM_LOST_ERROR);
    }

    @Override
    public int update(MasterDataInvoiceheaderVO_1 masterDataInvoiceheaderVO1) {
        if (masterDataInvoiceheaderVO1.getId() != null) {
            MasterDataInvoiceheaderVO_1 dbinvoiceheader = masterDataCInvoiceheaderMapper.selectById(masterDataInvoiceheaderVO1.getId());
            MasterDataCustomerVO dbcustomer = customerMapper.selectDetailBasic(dbinvoiceheader.getCustomerid());
            if (!ObjectUtil.isEmpty(dbcustomer)) {

                    DateTime date = DateUtil.date();
                    dbcustomer.setLastupdatetime(date.toString());
                    dbcustomer.setUserid(masterDataInvoiceheaderVO1.getUserid());
                    customerMapper.update(dbcustomer);
                    return masterDataCInvoiceheaderMapper.update(masterDataInvoiceheaderVO1);
                }
                throw new CustomException(ResultCodeEnum.ORGANIZATION_UPDATE_ERROR);

        }
        throw new CustomException(ResultCodeEnum.OPERATION_AUTHORITY_ERROR);
    }

    @Override
    public int delete(MasterDataInvoiceheaderVO_1 masterDataInvoiceheaderVO1) {
        MasterDataInvoiceheaderVO_1 dbinvoiceheader = masterDataCInvoiceheaderMapper.selectById(masterDataInvoiceheaderVO1.getId());
        if (!ObjectUtil.isEmpty(dbinvoiceheader)) {
            MasterDataCustomerVO dbcustomer = customerMapper.selectDetailBasic(dbinvoiceheader.getCustomerid());
            if(ObjectUtil.isEmpty(dbcustomer)){
                throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
            }
            if (dbinvoiceheader.getCustomerid().equals(dbcustomer.getId())) {
                DateTime date = DateUtil.date();
                dbcustomer.setLastupdatetime(date.toString());
                dbcustomer.setUserid(masterDataInvoiceheaderVO1.getUserid());
                customerMapper.update(dbcustomer);
                return masterDataCInvoiceheaderMapper.delete(masterDataInvoiceheaderVO1);
            }
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        throw new CustomException(ResultCodeEnum.OPERATION_AUTHORITY_ERROR);
    }

    /**
     * 分页查询
     */
    public PageInfo<MasterDataInvoiceheaderVO_1> selectPage(InvoiceheaderPageVO invoiceheaderVO, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MasterDataInvoiceheaderVO_1> list = masterDataCInvoiceheaderMapper.selectAll(invoiceheaderVO.getCustomerid());
        return PageInfo.of(list);
    }


}
